from typing import List
from pydantic import BaseModel, Field, root_validator
from ..common.trd_level import DateTime, User

"""
Belong:属于条款和用户协议部分
"""


class Navigation(BaseModel):
    """
    目录
    """
    id: int = Field(None, description='目录ID')
    name: str = Field(None, description='名称')
    level: str = Field(None, description='等级')
    parent_navigation: int = Field(None, description='上级目录')
    is_root: bool = Field(None, description='是否为根')
    active_flag: bool = Field(None, description='有效')

    @root_validator(pre=True)
    def create(cls, values):
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['name'] = mdl.name
            values['level'] = mdl.level
            values['parent_navigation'] = mdl.parent_navigation_id.id
            values['is_root'] = mdl.is_root
            values['active_flag'] = mdl.active_flag
        return values


class ComplianceAndAgreements(BaseModel):
    """
    条规和用户协议
    """
    id: int = Field(None, description='协议ID')
    code: str = Field(None, description='编码')
    user: User = Field(None, description='作者')
    type: str = Field(None, description='类型')
    create_time: str = Field(None, description='创建时间')
    publish_time: str = Field(None, description='发布时间')
    deprecation_time: str = Field(None, description='下架时间')
    disable_time: str = Field(None, description='弃用时间')
    title: str = Field(None, description='标题')
    sub_title: str = Field(None, description='副标题')
    introduction: str = Field(None, description='简介')
    body: str = Field(None, description='内容')
    navigation_records: List[Navigation] = Field(None, description='目录')
    publish_flag: bool = Field(None, description='发布')
    state: str = Field(None, description='状态')

    @root_validator(pre=True)
    def create(cls, values):
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['code'] = mdl.code
            values['user'] = User(__from_odoo=mdl.author, __env=env)
            values['type'] = mdl.type
            values['create_time'] = mdl.create_time.strftime('%Y-%m-%d %H:%M:%S') if mdl.create_time else None
            values['publish_time'] = mdl.publish_time.strftime('%Y-%m-%d %H:%M:%S') if mdl.publish_time else None
            values['deprecation_time'] = mdl.deprecation_time.strftime('%Y-%m-%d %H:%M:%S') if mdl.deprecation_time else None
            values['disable_time'] = mdl.disable_time.strftime('%Y-%m-%d %H:%M:%S') if mdl.disable_time else None
            values['title'] = mdl.title
            values['sub_title'] = mdl.sub_title
            values['introduction'] = mdl.introduction
            values['body'] = mdl.body
            values['navigation_records'] = [Navigation(__from_odoo=record, __env=env) for record in mdl.navigation_ids]
            values['publish_flag'] = mdl.publish_flag
            values['state'] = mdl.state
        return values
